package com.example.controller;

import com.example.entity.Dept;
import com.example.common.PageRequest;
import com.example.common.Result;
import com.example.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Object find(
            Dept dept,
            PageRequest pageRequest
    ) {
        System.out.println(dept);
        System.out.println(pageRequest);
        if (pageRequest.getPageSize() == null) {
            return Result.success(deptService.find(dept));
        } else {
            return Result.success(deptService.findPage(dept, pageRequest));
        }
    }

    @GetMapping("/{id}")
    public Object findOne(@PathVariable Integer id) {
        Dept dept = deptService.findOne(id);
        return Result.success(dept);
    }

    @PostMapping
    public Object insertOne(@RequestBody Dept dept) {
        deptService.insertOne(dept);
        return Result.success();
    }

    @PostMapping("/batch")
    public Object insertMany(@RequestBody List<Dept> deptList) {
        deptService.insertMany(deptList);
        return Result.success();
    }

    @PatchMapping("/{ids}")
    public Object updateMany(@PathVariable List<Integer> ids, @RequestBody Dept dept) {
        deptService.updateMany(ids, dept);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Object deleteMany(@PathVariable List<Integer> ids) {
        int updates = deptService.deleteMany(ids);
        return Result.success(updates);
    }
}

